325 页《图解深度学习》已开源,附全书代码!
以下文章来源于深度学习专栏 ,作者Will
点击上方“AI有道”,选择“置顶”公众号
重磅干货,第一时间送达
今天给大家介绍一个深度学习入门和进阶的绝佳教程:《Grokking Deep Learning》,中文译名为:《图解深度学习》。这本书是由 Manning 出版社出版,并采用 MEAP(订阅更新方式),从 2016 年 8 月开始,一直采用不定期更新的方式放送。时至今日,这本书终于完本啦,完结撒花。本书主打入门教学,书中各种插画丰富生动,是学习深度学习的入门好书。
作者简介
这本书的作者 Andrew Trask 是 DeepMind 的科学家,同时也是 OpenMinded的负责人,博士毕业于牛津大学。
个人主页是:https://iamtrask.github.io/
书籍简介
这本书会教你的从直觉的角度深入学习的基础知识,这样你就可以了解机器如何使用深度学习进行学习。这本书没有重点学习框架,如 Torch、TensorFlow 或 Keras。相反,它的重点是教你熟悉框架背后的深层次学习方法。一切都将从头开始,只使用 Python 和 NumPy。这样,你就能理解训练神经系统的每一个细节。网络,而不仅仅是如何使用代码库。你应该把这本书当作掌握其中一个主要框架的必要条件。
该书总共分为两大部分,第一部分是介绍神经网络的基础知识,总共包含 9 章内容:
第二部分是介绍深度学习中的高级层和架构,总共包含 7 章内容:
《图解深度学习》最大的特点就是在调包类书籍泛滥的当下,这本书可以说是非常良心了,作者通过 10 多章的铺垫,最终完成了一个微型的深度学习库,这应该也是本书的最大价值。
书籍资源
《图解深度学习》已经开放了在线版阅读并开源了书籍中所有的源代码。
在线阅读地址:
https://livebook.manning.com/#!/book/grokking-deep-learning/brief-contents/v-12/
代码地址:
https://github.com/iamtrask/Grokking-Deep-Learning
本书所有的代码实现都是基于 Python,并没有简单地调用库。这样能够最大程度地帮助你理解深度学习中的概念和原理。例如,CNN 模型的 Python 实现:
import numpy as np, sys
np.random.seed(1)
from keras.datasets import mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
images, labels = (x_train[0:1000].reshape(1000,28*28) / 255,
y_train[0:1000])
one_hot_labels = np.zeros((len(labels),10))
for i,l in enumerate(labels):
one_hot_labels[i][l] = 1
labels = one_hot_labels
test_images = x_test.reshape(len(x_test),28*28) / 255
test_labels = np.zeros((len(y_test),10))
for i,l in enumerate(y_test):
test_labels[i][l] = 1
def tanh(x):
return np.tanh(x)
def tanh2deriv(output):
return 1 - (output ** 2)
def softmax(x):
temp = np.exp(x)
return temp / np.sum(temp, axis=1, keepdims=True)
alpha, iterations = (2, 300)
pixels_per_image, num_labels = (784, 10)
batch_size = 128
input_rows = 28
input_cols = 28
kernel_rows = 3
kernel_cols = 3
num_kernels = 16
hidden_size = ((input_rows - kernel_rows) *
(input_cols - kernel_cols)) * num_kernels
# weights_0_1 = 0.02*np.random.random((pixels_per_image,hidden_size))-0.01
kernels = 0.02*np.random.random((kernel_rows*kernel_cols,
num_kernels))-0.01
weights_1_2 = 0.2*np.random.random((hidden_size,
num_labels)) - 0.1
def get_image_section(layer,row_from, row_to, col_from, col_to):
section = layer[:,row_from:row_to,col_from:col_to]
return section.reshape(-1,1,row_to-row_from, col_to-col_from)
for j in range(iterations):
correct_cnt = 0
for i in range(int(len(images) / batch_size)):
batch_start, batch_end=((i * batch_size),((i+1)*batch_size))
layer_0 = images[batch_start:batch_end]
layer_0 = layer_0.reshape(layer_0.shape[0],28,28)
layer_0.shape
sects = list()
for row_start in range(layer_0.shape[1]-kernel_rows):
for col_start in range(layer_0.shape[2] - kernel_cols):
sect = get_image_section(layer_0,
row_start,
row_start+kernel_rows,
col_start,
col_start+kernel_cols)
sects.append(sect)
expanded_input = np.concatenate(sects,axis=1)
es = expanded_input.shape
flattened_input = expanded_input.reshape(es[0]*es[1],-1)
kernel_output = flattened_input.dot(kernels)
layer_1 = tanh(kernel_output.reshape(es[0],-1))
dropout_mask = np.random.randint(2,size=layer_1.shape)
layer_1 *= dropout_mask * 2
layer_2 = softmax(np.dot(layer_1,weights_1_2))
for k in range(batch_size):
labelset = labels[batch_start+k:batch_start+k+1]
_inc = int(np.argmax(layer_2[k:k+1]) ==
np.argmax(labelset))
correct_cnt += _inc
layer_2_delta = (labels[batch_start:batch_end]-layer_2)\
/ (batch_size * layer_2.shape[0])
layer_1_delta = layer_2_delta.dot(weights_1_2.T) * \
tanh2deriv(layer_1)
layer_1_delta *= dropout_mask
weights_1_2 += alpha * layer_1.T.dot(layer_2_delta)
l1d_reshape = layer_1_delta.reshape(kernel_output.shape)
k_update = flattened_input.T.dot(l1d_reshape)
kernels -= alpha * k_update
test_correct_cnt = 0
for i in range(len(test_images)):
layer_0 = test_images[i:i+1]
# layer_1 = tanh(np.dot(layer_0,weights_0_1))
layer_0 = layer_0.reshape(layer_0.shape[0],28,28)
layer_0.shape
sects = list()
for row_start in range(layer_0.shape[1]-kernel_rows):
for col_start in range(layer_0.shape[2] - kernel_cols):
sect = get_image_section(layer_0,
row_start,
row_start+kernel_rows,
col_start,
col_start+kernel_cols)
sects.append(sect)
expanded_input = np.concatenate(sects,axis=1)
es = expanded_input.shape
flattened_input = expanded_input.reshape(es[0]*es[1],-1)
kernel_output = flattened_input.dot(kernels)
layer_1 = tanh(kernel_output.reshape(es[0],-1))
layer_2 = np.dot(layer_1,weights_1_2)
test_correct_cnt += int(np.argmax(layer_2) ==
np.argmax(test_labels[i:i+1]))
if(j % 1 == 0):
sys.stdout.write("\n"+ \
"I:" + str(j) + \
" Test-Acc:"+str(test_correct_cnt/float(len(test_images)))+\
" Train-Acc:" + str(correct_cnt/float(len(images))))
资源下载
最后,本书的的前 11 章电子版 pdf 和所有源代码已经打包完毕,需要的可以在本公众号后台回复关键字:GDL,直接获取!
【推荐阅读】
你正在看吗? 👇